package com.balabala.gym_system.service.impl;

import com.balabala.gym_system.mapper.RoleMapper;
import com.balabala.gym_system.pojo.dto.RoleQueryDto;
import com.balabala.gym_system.pojo.dto.UpdateRoleMenuDto;
import com.balabala.gym_system.pojo.entity.Role;
import com.balabala.gym_system.pojo.entity.RoleMenu;
import com.balabala.gym_system.pojo.vo.RoleListVo;
import com.balabala.gym_system.service.RoleMenuService;
import com.balabala.gym_system.service.RoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Author:zl
 * Date:2023/5/1 16:55
 */

@Service
@Slf4j
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMenuService roleMenuService;

    @Override
    public void updateRoleMenu(UpdateRoleMenuDto updateRoleMenuDto) {
        // 删除当前角色所有菜单
        Long roleId = updateRoleMenuDto.getId();

        roleMenuService.remove(new QueryWrapper<RoleMenu>().eq("role_id", roleId));
        if (updateRoleMenuDto.getMenuIds().size() > 0) {
            // 批量插入 role_menu
            List<Long> menuIds = updateRoleMenuDto.getMenuIds();
            List<RoleMenu> roleMenus = new ArrayList<RoleMenu>();
            for (Long menuId : menuIds) {
                roleMenus.add(new RoleMenu(roleId, menuId));
            }
            roleMenuService.saveBatch(roleMenus, roleMenus.size());
        }
    }

    @Override
    public List<Role> findRoleByAdminId(Long adminId) {
        return getBaseMapper().findRoleByAdminId(adminId);
    }
}

